﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web;
using System.Globalization;
using Ribbon.Web.Helper;
using System.Web.UI.WebControls;
using AjaxControlToolkit;

namespace Ribbon.Web
{
    // TODO: find an ulternative way to set colors insted of background image
    public enum MenuColor
    {
        [Description("Ribbon_FileMenu_green")]
        Green,
        [Description("Ribbon_FileMenu_orange")]
        Orange,
        [Description("Ribbon_FileMenu_blue")]
        Blue,
        [Description("Ribbon_FileMenu_purple")]
        Purple
    }

    // TODO: will be upgraeded to be real menu.
    [ParseChildren(typeof(RibbonItem))]
    public class RibbonMenu : System.Web.UI.WebControls.CompositeControl
    {
        private Panel _popupContainer;
        private PopupControlExtender _popup;

        public RibbonMenu()
        {
            _popup = new PopupControlExtender();
            _popupContainer = new Panel();
        }

        public string Text
        {
            set
            {
                this.ViewState["Text"] = value;
            }
            get
            {
                return (this.ViewState["Text"] as string) ?? string.Empty;
            }
        }
        public MenuColor Color
        {
            set
            {
                this.ViewState["Color"] = value;
            }
            get
            {
                var obj = this.ViewState["Color"];
                return obj == null ? MenuColor.Green : (MenuColor)obj;
            }
        }
        
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            this.CssClass += " " + ExtractClass(this.Color);
            this.CssClass += " Ribbon_FileMenu";


            writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.Write(Text);
        }
        protected override void AddParsedSubObject(object obj)
        {
            var objRibbonZone = obj as RibbonGroup;
            if (null != objRibbonZone)
            {
                Controls.Add(objRibbonZone);
            }
            else if (!(obj is LiteralControl))
            {
                throw new HttpException(string.Format(CultureInfo.CurrentCulture, "RibbonMenu cannot have children of type '{0}'.", obj.GetType()));
            }
        }
        protected override ControlCollection CreateControlCollection()
        {
            return new ControlCollection<RibbonMenu, RibbonItem>(this);
        }
        protected override void CreateChildControls()
        {
            _popup.TargetControlID = this.ID;
            _popup.Position = PopupControlPopupPosition.Bottom;
            _popup.PopupControlID = this._popupContainer.ID;
            //--------------------------------------------------------------------------------

        }
        private string ExtractClass(MenuColor value)
        {
            var member = typeof(MenuColor).GetMember(value.ToString())[0];
            var attr = (DescriptionAttribute)Attribute.GetCustomAttribute(member, typeof(DescriptionAttribute));
            return attr.Description;
        }
    }
}
